package com.fone.core.adaptor;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.DisplayMetrics;

import com.fone.player.ApplicationManage;
import com.fone.player.play.adapter.FonePlayerAdapter;
import com.fone.player.storage.StorageModule;
import com.fone.player.storage.download.DownloadUtil;
import com.fone.player.util.L;
import com.fone.player.util.SystemUtil;

public class JNIUtil {

	private static final String TAG = "JNIUtil";

	public static int getSdkVersion() {
		int v = android.os.Build.VERSION.SDK_INT;
		return v;
	}

	public static int getScreenHeight() {
		Context context = ApplicationManage.getGlobalContext();
		if (context != null) {
			DisplayMetrics metrics = context.getResources().getDisplayMetrics();
			int width = metrics.widthPixels;
			int height = metrics.heightPixels;
			return (width > height ? width : height);
		} else {
			L.v("JNIUtil", "getScreenHeight",
					"getCipher context is null. who did it?");
			return 800;
		}
	}

	public static int getScreenWidth() {
		Context context = ApplicationManage.getGlobalContext();
		if (context != null) {
			DisplayMetrics metrics = context.getResources().getDisplayMetrics();
			int width = metrics.widthPixels;
			int height = metrics.heightPixels;
			return (width < height ? width : height);
		} else {
			L.v(TAG, "getScreenWidth", "getCipher context is null. who did it?");
			return 480;
		}
	}

	/**
	 * 
	 * 判断网络连接类型(C层会调用该方法,修改名称后,请通知郑建明,否则调用异常)
	 * 
	 * @return int 网络连接类型<br>
	 *         0 无网<br>
	 *         1 wifi网<br>
	 *         2 2G网<br>
	 *         3 3G网<br>
	 *         4 4G网<br>
	 */
	public static int getNetworkType() {
		int netType = DownloadUtil.getNetType();
		int simpleType = 0;
		switch (netType) {
		case StorageModule.MSG_NO_NETWORK_TYPE:
			simpleType = 0;
			break;
		case StorageModule.MSG_WIFI_NETWORK_TYPE:
			simpleType = 1;
			break;
		case StorageModule.MSG_2G_NETWORK_TYPE:
			simpleType = 2;
			break;
		case StorageModule.MSG_3G_NETWORK_TYPE:
			simpleType = 3;
			break;
		case StorageModule.MSG_4G_NETWORK_TYPE:
			simpleType = 4;
			break;
		default:
			break;
		}
		return simpleType;
	}

	/**
	 * 
	 * 返回缓存路径(C层会调用该方法,修改名称后,请通知郑建明,否则调用异常)
	 * 
	 * @return String 缓存路径
	 */
	public static String getStorePath() {

		String path = Environment.getExternalStorageDirectory()
				+ "/100tv/cache";
		// 如果路径不存在,则创建
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		L.v(TAG, "getStorePath", "path=" + path);
		return path;
	}

	/**
	 * 
	 * 返回缓存路径(C层会调用该方法,修改名称后,请通知郑建明,否则调用异常)
	 * 
	 * @return String 缓存路径
	 */
	public static String getStorePath(int type) {

		String path = SystemUtil.getFoneCacheFolder(type);
		L.v(TAG, "getStorePath(int type)", "path=" + path);
		return path;
	}

	/**
	 * 是否支持省电加速（底层回调）
	 * 
	 * @return
	 * @return int 1 YES , 0 NO
	 * @throws
	 */
	public static int get_setting_use_hw_decoder() {
		L.v(TAG,
				"get_setting_use_hw_decoder >>>>>>>"
						+ FonePlayerAdapter.getHwPlusSupport());
		return FonePlayerAdapter.getHwPlusSupport();
	}

	// return 1 YES , 0 NO
	public static int get_setting_use_sys_decoder() {
		L.v(TAG, "get_setting_use_sys_decoder", ">>>>>>>");
		return FonePlayerAdapter.getSystem_decoder();
	}

	public static Object Bitmapbuffer_Create(int width, int height) {
		if (width > 0 && height > 0) {
			try {
				Object obj = Bitmap.createBitmap(width, height,
						Bitmap.Config.RGB_565);
				return obj;
			} catch (Throwable e)// Exception e
			{
				e.printStackTrace();
				return null;
			}
		}
		return null;
	}
}